//app.ts
import Koa from 'koa'; // 导入koa
import KoaLogger from 'koa-logger'; // 导入日志
import path from 'path';
const views = require('koa-views');
const app = new Koa(); // 新建一个koa对象
const routerUser = require('./routes/users');
const routerIndex = require('./routes/index');
const logger = KoaLogger();
require('module-alias/register'); // 导入路径简写
app.use(logger);
app.on("error", (err:any, ctx:any) => {//捕获异常记录错误日志
  console.log(new Date(), ":", err)
})
// 必须在router上
const staticPath = path.join(__dirname, '../public'); // 静态地址
const viewsPath = path.join(__dirname, '../views'); // 模板地址
app.use(require('koa-static')(staticPath));
app.use(views(viewsPath, {
  extension: 'pug'
}));
app.use(routerIndex.routes());
app.use(routerUser.routes());
 
module.exports = app;